package com.dongly.Insertion;

import com.dongly.Sort;
import com.dongly.SortTestHelper;

import java.util.Arrays;

/**
 * 插入排序
 *
 * @auther DLY
 * @create 2017/9/16
 */
public class InsertSort implements Sort<Integer> {

    public static void main(String[] args) {
        Integer[] array = SortTestHelper.generateRandomArray(10, 1, 10);

        SortTestHelper.testSort(new InsertSort(), array);
        System.out.println(Arrays.toString(array));
    }

    @Override
    public void sort(Integer[] integers) {
        Integer temp;
        for (int i = 1, len = integers.length; i < len; i++) {
            temp = integers[i];
            int j;
            for (j = i; j > 0; j--) {
                if (integers[j - 1].compareTo(temp) > 0) {
                    integers[j] = integers[j - 1];
                } else {
                    break;
                }
            }
            integers[j] = temp;
        }
    }
}
